<?php
/*
 * MySQL Adapter
 */
 
class MySqlAdapter {
	
	protected $dbConn, $host, $user, $password, $dbName, $port; 
	public $errno, $error, $insertId, $affectedRows, $numRows;
	
	public function __construct($dbcPar) {
		if (!$this->dbConn) {
			$this->dbConn = mysql_pconnect(
				$dbcPar['host'].':'.$dbcPar['port'], 
				$dbcPar['user'],
				$dbcPar['password']);
			if (mysql_errno()) {
				die('Unable to connect: **' . mysql_error().' ** '.print_r($dbcPar,1));
			}
			mysql_select_db($dbcPar['dbName']);
			if (mysql_errno()) {
				die('Unable to select DB: ' . mysqli_error().' ** '.print_r($dbcPar,1));
			}
		}
	}	
	
 	function query($sql, $aOpt='') {
		$query = mysql_query($sql);
		if (mysql_errno()) {
			die(mysql_error()."\n<br> ".$sql);
		}
 		return $query;
 	}
	
 	function fetch($query,$pos='') {
 		$row = mysql_fetch_assoc($query);	
 		return $row;
 	}
	
 	function insert($entita, $aAttrib, $alias, $aOpt='') {
 		$sql = "INSERT INTO ".$entita." SET ";
 		foreach($aAttrib as $nomeAttr => $valAttr) {
 			if ($nomeAttr[0] != $alias) { continue; }
 			$sql .= substr($nomeAttr,2)." = '".addslashes($valAttr)."', "; 
 		}
 		$sql = substr($sql,0,-2);
		$query = mysql_query($sql);
		$this->errno = mysql_errno($this->dbConn);
		$this->error = mysql_error($this->dbConn);	
		$this->insertId = mysql_insert_id($this->dbConn);	
		mysql_free_result($query);
		if ($this->errno != 0) return false;
		return true;
 	}
	
 	function update($entita, $aAttrib, $alias, $aKey, $aOpt='') {
 		$sql = "UPDATE ".$entita." SET ";
 		foreach($aAttrib as $nomeAttr => $valAttr) {
 			if ($nomeAttr[0] != $alias) { continue; }
 			if (in_array($nomeAttr,$aKey)) { continue; }
 			$sql .= substr($nomeAttr,2)." = '".addslashes($valAttr)."', "; 
 		}
 		$sql = substr($sql,0,-2)." WHERE ";
 		foreach($aKey as $kk => $nomeAttr) {
 			$sql .= substr($nomeAttr,2)." = '".addslashes($aAttrib[$nomeAttr])."' AND "; 
 		}
 		$sql = substr($sql,0,-5);
		$query = mysql_query($sql);
		$this->errno = mysql_errno($this->dbConn);
		$this->error = mysql_error($this->dbConn);	
		if ($this->errno != 0) return false;
		$this->affectedRows = mysql_affected_rows($this->dbConn);
		return true;
 	}
 	
 	function delete($entita, $aAttrib, $aKey, $aOpt='') {
 		$sql = "DELETE FROM ".$entita." WHERE ";
 		foreach($aKey as $kk => $nomeAttr) {
 			$sql .= substr($nomeAttr,2)." = '".addslashes($aAttrib[$nomeAttr])."' AND "; 
 		}
 		$sql = substr($sql,0,-5);
		$query = mysql_query($sql);
		$this->errno = mysql_errno($this->dbConn);
		$this->error = mysql_error($this->dbConn);	
		if ($this->errno != 0) return false;
		$this->affectedRows = mysql_affected_rows($this->dbConn);
		return true;
 	}
 	
 	function read($entita, &$aAttrib, $alias, $aKey, $aOpt='') {
 		// $aOpt['lock'] = 'yes' : esegue select with lock
 		// $aOpt['locktype'] = "upd" : lock type per Update 		
 		// $aOpt['locktype'] = "shr" : lock type tipo shared
 		$sql = "SELECT ";
		foreach($aAttrib as $nomeAttr => $valAttr) {
 			if ($nomeAttr[0] != $alias) { continue; }
 			//if (in_array($nomeAttr,$aKey)) { continue; }
 			$sql .= substr($nomeAttr,2).", "; 
 		}
 		
		$sql = substr($sql,0,-2)." FROM ".$entita." WHERE ";
 		foreach($aKey as $kk => $nomeAttr) {
 			$sql .= substr($nomeAttr,2)." = '".addslashes($aAttrib[$nomeAttr])."' AND "; 
 		}
 		$sql = substr($sql,0,-5);
 		if(isset($aOpt['lock']) && $aOpt['lock'] == "yes"){
 			if(isset($aOpt['locktype']) && $aOpt['locktype'] == "upd")
 				$sql .= " FOR UPDATE";
 			if(isset($aOpt['locktype']) && $aOpt['locktype'] == "shr")
 				$sql .= " LOCK IN SHARE MODE";
 		}
 		echo("<br>SQL: $sql<br><br>");
		$query = mysql_query($sql);
		$row = $this->fetch($query);
		if($row){
			foreach($aAttrib as $nomeAttr => $valAttr) {
	 			if ($nomeAttr[0] != $alias) { continue; }
	 			if (in_array($nomeAttr,$aKey)) { continue; }
	 			$aAttrib[$nomeAttr] = $row[substr($nomeAttr,2)]; 
	 		}
		}			
		$this->errno = mysql_errno($this->dbConn);
		$this->error = mysql_error($this->dbConn);	
		if ($this->errno != 0) return false;
		$this->affectedRows = mysql_affected_rows($this->dbConn);
		mysql_free_result($query);
		return true;
 	}
 	
 	function getFields($entita){
 		$sSQL = "SHOW COLUMNS FROM $entita";
 		$aFields = array();
 		if($query = $this->query($sSQL)){
 			while($row = $this->fetch($query)){
 				$aFields[$row["Field"]] = array("Type"=>trim($row["Type"])); 
 			}
 			return $aFields;
 		}else{
 			return false;
 		}
 	}
 	
 	function startTransaction() {
 		$sSQL = "SET AUTOCOMMIT=0";
 		if($this->query($sSQL)){
 			return true;
 		}else{ 				
	 		$this->errno = mysql_errno($this->dbConn);
			$this->error = mysql_error($this->dbConn);	
			return false;	
 		}
 	}
 	
 	function commit() {
 		$sSQL = "COMMIT";
 		if($this->query($sSQL)){
 			return true;
 		}else{ 				
	 		$this->errno = mysql_errno($this->dbConn);
			$this->error = mysql_error($this->dbConn);	
			return false;	
 		}
 	}
 	
 	function rollback() {
 		$sSQL = "ROLLBACK";
 		if($this->query($sSQL)){
 			return true;
 		}else{ 				
	 		$this->errno = mysql_errno($this->dbConn);
			$this->error = mysql_error($this->dbConn);	
			return false;	
 		}
 	}
}
?>
